export const state = () => {
  let paySeq = sessionStorage.getItem("pay-seq");
  let currencys = sessionStorage.getItem("currencys");

  let user = localStorage.getItem('user')
  let uid = localStorage.getItem('uid')
  let token = localStorage.getItem('token');

  try{
    user = JSON.parse(user);
  }catch(e){
    user = null;
  }

  try {
    paySeq = JSON.parse(paySeq);
  } catch (e) {
    paySeq = null;
  }

  try {
    currencys = JSON.parse(currencys);
  } catch (e) {
    currencys = [];
  }

  return {
    user,
    token,
    uid,
    currencys, //币种
    paySeq,
    wallets:null //钱包信息
  }
}

export const mutations = {
  SET_USER (state, user) {
    // 每次保存后缓存到本地
    if(user){
      localStorage.setItem("user",JSON.stringify(user));
    }else{
      localStorage.removeItem("user");
    }
    state.user = user || null
  },
  SET_UID (state,uid){
    // 每次保存后缓存到本地
    if(uid){
      localStorage.setItem("uid",uid);
    }else{
      localStorage.removeItem("uid");
    }
    state.uid = uid;
  },
  SET_TOKEN (state,token){
    if(token){
      localStorage.setItem("token",token);
    }else{
      localStorage.removeItem("token");
    }
    state.token = token;
  },
  SET_CURRENCYS(state,data){
    try{
      if(data){
        sessionStorage.setItem('currencys',JSON.stringify(data));
      }else{
        data = []
        sessionStorage.removeItem("currencys");
      }
    }catch(e){
      data = []
    }

    state.currencys = data
  },
  SET_PAY_SEQ(state,data){ //获取用户币种和扣费顺序
    try{
      if(data){
        sessionStorage.setItem('pay-seq',JSON.stringify(data));
      }else{
        data = null
        sessionStorage.removeItem("pay-seq");
      }
    }catch(e){
      data = null
    }

    state.paySeq = data
  },
  SET_WALLETS(state,data){
    state.wallets = data;
  }
}

export const getters = {
  isAuthenticated (state) {
    return !!state.user
  },
  getUser (state) {
    return state.user
  },
  getToken (state){
    return state.token;
  },
  getUid (state) {
    return state.uid;
  }
}

export const actions = {
  LOGOUT({commit}){
    //退出登录清掉缓存
    sessionStorage.clear();
    commit('SET_USER',null)
    commit('SET_UID','')
    commit('SET_TOKEN','')
  },
  async GET_USER ({commit}) {
    try {
      let { $axios }  = this.app
      let { data:rs }  = await $axios.post('?ct=api&ac=get_user');
      if(rs.code === 0){
        commit('SET_USER',rs.data)
        commit('SET_UID',rs.data.uid)
      }
    } catch (e) {
      throw e;
    }
  },
  async GET_CURRENCYS ({commit}) { //获取币种信息
    try {
      let { data:rs } = await this.$axios.post('?ct=api&ac=get_currencys');
      if(rs.code === 0){
        commit('SET_CURRENCYS',rs.data.list)
      }
    }catch(e){
      throw e;
    }
  },
  async GET_RATE ({commit}) { //获取汇率
    try {
      let {data:rs} = await this.$axios.post('?ct=api&ac=get_rate');
    }catch(e){
      this.$error(e);
    }
  },

  async GET_PAY_SEQ({commit}) { //获取用户的扣费顺序
    try {
      let { data:rs } = await this.$axios.post('?ct=api&ac=get_pay_seq');
      if(rs.code == 0){
        commit('SET_PAY_SEQ',rs.data)
      }
    }catch(e){
      this.$error(e);
    }
  },

  async GET_WALLETS({commit}) { // 获取用户钱包信息
    try{
      let {data:rs} = await this.$axios.post('?ct=api&ac=check_wallets');
      if(rs.code == 0){
        commit('SET_WALLETS',rs.data.wallets)
      }else{
        this.$toast(rs.msg);
      }
    }catch(e){
      this.$toast('获取钱包数据失败');
      this.$error(e);
    }
  }
}
